package com.ktgj.other.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.other.domain.*;
import com.ktgj.other.domain.common.DayPriceRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.other.service.IRcOrderService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 专车订单Controller
 * 
 * @author ruoyi
 * @date 2022-07-27
 */
@RestController
@RequestMapping("/rcOrder")
public class RcOrderController extends BaseController
{
    @Autowired
    private IRcOrderService rcOrderService;

    /**
     * 查询专车订单列表
     */
    //@PreAuthorize(hasPermi = "other:order:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody RcOrder rcOrder)
    {
        startPage();
        List<RcOrder> list = rcOrderService.selectRcOrderList(rcOrder);
        return getDataTable(list);
    }

    /**
     * 专车订单列表
     * @param rcOrder
     * @return
     */
    @PostMapping("/queryRcOrderList")
    public TableDataInfo queryRcOrderList(@RequestBody RcOrder rcOrder) {
        startPage();
        List<RcOrder> rcOrders = rcOrderService.queryRcOrderList(rcOrder);
        return getDataTable(rcOrders);
    }

    /**
     * 导出专车订单列表
     */
    @PreAuthorize(hasPermi = "other:order:export")
    @Log(title = "专车订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RcOrder rcOrder) throws IOException
    {
        List<RcOrder> list = rcOrderService.selectRcOrderList(rcOrder);
        ExcelUtil<RcOrder> util = new ExcelUtil<RcOrder>(RcOrder.class);
        util.exportExcel(response, list, "专车订单数据");
    }

    /**
     * 获取专车订单详细信息
     */
    @PreAuthorize(hasPermi = "other:order:query")
    @GetMapping(value = "/{rcorderid}")
    public AjaxResult getInfo(@PathVariable("rcorderid") Long rcorderid)
    {
        return AjaxResult.success(rcOrderService.selectRcOrderById(rcorderid));
    }

    /**
     * 新增专车订单
     */
    @PreAuthorize(hasPermi = "other:order:add")
    @Log(title = "专车订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RcOrder rcOrder)
    {
        return toAjax(rcOrderService.insertRcOrder(rcOrder));
    }

    /**
     * 修改专车订单
     */
    @PreAuthorize(hasPermi = "other:order:edit")
    @Log(title = "专车订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RcOrder rcOrder)
    {
        return toAjax(rcOrderService.updateRcOrder(rcOrder));
    }

    /**
     * 删除专车订单
     */
    @PreAuthorize(hasPermi = "other:order:remove")
    @Log(title = "专车订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rcorderids}")
    public AjaxResult remove(@PathVariable Long[] rcorderids)
    {
        return toAjax(rcOrderService.deleteRcOrderByIds(rcorderids));
    }

    /**
     * 订单填写
     * @param servletRequest
     * @param request
     * @return
     */
    @PostMapping("/fillOrder")
    public AjaxResult fillOrder(HttpServletRequest servletRequest, @RequestBody DayPriceRequest request) {
        String token = servletRequest.getHeader("token");
        RcFillOrder order = null;
        try {
            order = rcOrderService.fillOrder(token, request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(order);
    }

    /**
     * 创建订单
     * @param request
     * @param rcOrder
     * @return
     */
    @PostMapping("/createOrder")
    public AjaxResult createOrder(HttpServletRequest request, @RequestBody RcOrder rcOrder) {
        String order;
        String token = request.getHeader("token");
        try {
            if (token != null)
                order = rcOrderService.createOrder(token, rcOrder);
            else
                order = rcOrderService.createOrder(null, rcOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (order != null)
            return AjaxResult.success("订单创建成功", order);
        else
            return AjaxResult.error("订单创建失败");
    }

    /**
     * 订单收银台
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/cashier")
    public AjaxResult cashier(HttpServletRequest request, @RequestBody Map<?, ?> map) {
        String token = request.getHeader("token");
        String orderId = String.valueOf(map.get("orderId"));
        return AjaxResult.success(rcOrderService.cashier(token, orderId));
    }

    /**
     * 专车订单列表
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/rcOrderList")
    public AjaxResult rcOrderList(HttpServletRequest request, @RequestBody Map<String, Long> map) {
        List<RcOrderList> rcOrderLists;
        String token = request.getHeader("token");
        Long orderstatus = map.get("orderstatus");
        try {
            if (orderstatus != null)
                rcOrderLists = rcOrderService.rcOrderList(token, orderstatus);
            else
                return AjaxResult.error("订单状态不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(rcOrderLists);
    }

    /**
     * 订单详情-用车信息
     * @param map
     * @return
     */
    @PostMapping("/carInformation")
    public AjaxResult carInformation(@RequestBody Map<String, String> map) {
        RcOrderCarInformation information;
        if (map.get("orderid") != null && !map.get("orderid").equals("")) {
            information = rcOrderService.carInformation(map.get("orderid"));
        } else {
            return AjaxResult.error("订单号不能为空");
        }
        return AjaxResult.success(information);
    }

    /**
     * 专车银行卡支付
     * @param bankCardPay
     * @return
     */
    @PostMapping("/bankCardPay")
    public AjaxResult bankCardPay(HttpServletRequest request, @RequestBody RcOrderBankCardPay bankCardPay) {
        String cardPay = null;
        try {
            cardPay = rcOrderService.bankCardPay(bankCardPay, request);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(cardPay);
    }

    /**
     * 专车订单详情
     * @param map
     * @return
     */
    @PostMapping("/rcOrderDetail")
    public AjaxResult rcOrderDetail(@RequestBody Map<?, ?> map) {
        String orderid = String.valueOf(map.get("orderid"));
        return AjaxResult.success(rcOrderService.rcOrderDetail(orderid));
    }
}
